package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Like;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.LikeMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LikeMapper likeMapper;


    public User register(User user) {
        //查询回这个User是否存在
        User temp = userMapper.selectByEmailAndPassword(user.getEmail(), null);
        if (temp == null&&user.getPassword()!=null) {
            user.setHeadImgUrl("/upload/f27fa671-77db-4cf8-a80f-17b6687a3bcd.jpeg");
            user.setPlace("广州");
            user.setGender(1);
            user.setCoverImgUrl("/img/user/bg.jpeg");
            userMapper.insert(user);
        }else if (temp != null){
            throw new RuntimeException("邮箱已存在,请更换");
        }
        return temp;
    }

    @Override
    public User login(String email, String password) {
        User temp = userMapper.selectByEmailAndPassword(email, password);
        if (temp == null) {
            throw new RuntimeException("账号或密码错误");
        }else {
            return temp;
        }
    }

    @Override
    public User update(User user) {
        userMapper.updateByPrimaryKey(user);
        User user1 = userMapper.selectByPrimaryKey(user.getId());

        return user1;
    }

    @Override
    public PageInfo getList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list=userMapper.getList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Like> selectLikeByAuthorId(Long id) {
        List<Like> likes = likeMapper.selectByAuthorId(id);
        return likes;
    }

    @Override
    public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void putFans(Long id) {
        //获得当前的作者信息
        User author = userMapper.selectByPrimaryKey(id);
        //当前作者的关注数
        Integer fans = author.getFans();

        //获得当前登录用户信息
        User user = UserContext.getCurrentUser();
        Integer attention = user.getAttention();

        if (author.getState() == 0) {
            fans += 1;
            author.setState(1);

            attention += 1;
            userMapper.insertRelation(id,user.getId());
        } else if (author.getState() == 1) {
            fans = fans - 1;
            author.setState(0);

            attention = attention - 1;
            userMapper.deleteRelation(id,user.getId());
        }

        author.setFans(fans);
        userMapper.updateFans(author);

        user.setAttention(attention);
        userMapper.updateFans(user);
    }

    @Override
    public List<User> getFans(Long id) {
        return userMapper.getFans(id);
    }

    @Override
    public List<User> getAttention(Long id) {
        return userMapper.getAttention(id);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int getNoReadLikeByAuthorId(Long id) {
        return likeMapper.getNoReadLikeByAuthorId(id);
    }

    @Override
    public void setLikeHasReadByAuthorId(Long id) {
        likeMapper.setLikeHasReadByAuthorId(id);
    }

}
